package com.cn.yugan.Services;

import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.support.annotation.Nullable;

import com.cn.yugan.Engine.Http.AddReqCookie;
import com.cn.yugan.Engine.Http.Nocation;
import com.cn.yugan.Engine.Json.NoticeMessageJsonObj;
import com.cn.yugan.Engine.Json.NoticeNormalMessageJsonObj;
import com.cn.yugan.Utils.LogUtils;
import com.loopj.android.http.AsyncHttpClient;

public class NoticeMessageService extends Service {

    private AsyncHttpClient asyncHttpClient;

    public Handler mhandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case Nocation.NOTICE_ALL:
                    if (((NoticeNormalMessageJsonObj) (msg.obj)).getUnread() > 0) {
                        Nocation.getnotice(NoticeMessageService.this, Nocation.NOTICE_ATME, asyncHttpClient, mhandler);
                        Nocation.getnotice(NoticeMessageService.this, Nocation.NOTICE_REPLYME, asyncHttpClient, mhandler);
                        Nocation.getnotice(NoticeMessageService.this, Nocation.NOTICE_MESSAGE, asyncHttpClient, mhandler);
                        //发送广播
                        Intent intent = new Intent();
                        intent.putExtra("count", ((NoticeNormalMessageJsonObj) (msg.obj)).getUnread());
                        intent.setAction("com.yugan.android.phone.NoticeMessageService");
                        sendBroadcast(intent);
                        LogUtils.e("广播已发送");
                    }
                    break;
                case Nocation.NOTICE_ATME:

                    break;
                case Nocation.NOTICE_REPLYME:

                    break;
                case Nocation.NOTICE_MESSAGE:

                    break;

                case Nocation.NOTICE_LOOPER:
                    Nocation.getnotice(NoticeMessageService.this, Nocation.NOTICE_ALL, asyncHttpClient, mhandler);
                    break;

            }


            super.handleMessage(msg);
        }


    };


    public NoticeMessageService() {

    }

    /**
     * 创建后每60秒获取一次notice
     */
    @Override
    public void onCreate() {
        super.onCreate();
        LogUtils.e("服务已创建");
        init();

        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    try {
                        Message msg = Message.obtain();
                        msg.what = Nocation.NOTICE_LOOPER;
                        mhandler.sendMessage(msg);
                        Thread.sleep(60000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();

    }

    private void init() {
        asyncHttpClient = new AsyncHttpClient(true,80,443);
        asyncHttpClient.addHeader("cookie", AddReqCookie.getNormalheader(this));
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
}
